package com.ruoyi.cms.controller;

import java.util.List;

import com.ruoyi.cms.domain.CDisplay;
import com.ruoyi.cms.dto.CityDisplayDTO;
import com.ruoyi.cms.vo.DisplayVO;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CCity;
import com.ruoyi.cms.service.ICCityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 城市管理Controller
 * 
 * @author Choi
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/cms/city")
public class CCityController extends BaseController
{
    @Autowired
    private ICCityService cCityService;
    @Autowired
    private TokenService tokenService;

    /**
     * 取消关联
     */
    @PreAuthorize("@ss.hasPermi('cms:city:displays:delete')")
    @DeleteMapping("/{id}/displays")
    public AjaxResult deleteDisplay(@PathVariable long id,
                                    @RequestBody @Validated long[] displayIds) {
        if (displayIds != null && displayIds.length > 0) {
            cCityService.deleteCityDisplay(id, displayIds);
        }
        return AjaxResult.success();
    }

    /**
     * 修改关联排序
     */
    @PreAuthorize("@ss.hasPermi('cms:city:displays:edit')")
    @PutMapping("/displays")
    public AjaxResult editDisplay(@RequestBody @Validated CityDisplayDTO cityDisplayDTO) {
        cCityService.editCityDisplay(cityDisplayDTO);
        return AjaxResult.success();
    }

    /**
     * 添加关联
     */
    @PreAuthorize("@ss.hasPermi('cms:city:displays:add')")
    @PostMapping("/displays")
    public AjaxResult addDisplay(@RequestBody @Validated List<CityDisplayDTO> list) {
        if (list != null && list.size() > 0) {
            cCityService.addCityDisplay(list);
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取所有展示屏
     */
    @PreAuthorize("@ss.hasPermi('cms:city:displays')")
    @GetMapping("/{id}/displays")
    public AjaxResult displays(@PathVariable long id) {
        List<DisplayVO> displays = cCityService.getAllDisplays(id);
        return AjaxResult.success(displays);
    }

    /**
     * 关联展示屏
     */
    @PreAuthorize("@ss.hasPermi('cms:city:displays')")
    @GetMapping("/{id}/display")
    public AjaxResult displayList(@PathVariable long id) {
        List<DisplayVO> displays = cCityService.getDisplayList(id);
        return AjaxResult.success(displays);
    }

    /**
     * 查询城市管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(CCity cCity)
    {
        startPage();
        List<CCity> list = cCityService.selectCCityList(cCity);
        return getDataTable(list);
    }

    /**
     * 导出城市管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:city:export')")
    @Log(title = "城市管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CCity cCity)
    {
        List<CCity> list = cCityService.selectCCityList(cCity);
        ExcelUtil<CCity> util = new ExcelUtil<CCity>(CCity.class);
        return util.exportExcel(list, "城市管理数据");
    }

    /**
     * 获取城市管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:city:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cCityService.selectCCityById(id));
    }

    /**
     * 新增城市管理
     */
    @PreAuthorize("@ss.hasPermi('cms:city:add')")
    @Log(title = "城市管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CCity cCity)
    {
        return toAjax(cCityService.insertCCity(cCity));
    }

    /**
     * 修改城市管理
     */
    @PreAuthorize("@ss.hasPermi('cms:city:edit')")
    @Log(title = "城市管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CCity cCity)
    {
        return toAjax(cCityService.updateCCity(cCity));
    }

    /**
     * 删除城市管理
     */
    @PreAuthorize("@ss.hasPermi('cms:city:remove')")
    @Log(title = "城市管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cCityService.deleteCCityByIds(ids));
    }
}
